package com.test.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.test.bean.TextMeaasge;

/**
 * 实现消息的格式转换(Map类型和XML的互转)
 */
public class WeiXinMsgUtil
{
	public static String access_token;
	public static Date access_token_date;
	public static long accessTokenInvalidTime = 2 * 60 * 60 * 1000;
	public static String AppID = "wxa993324af2a33bce";
	public static String AppSecret;

	// 各种消息类型,除了扫带二维码事件
	/**
	 * 文本消息
	 */
	public static final String MESSAGE_TEXT = "text";
	/**
	 * 图片消息
	 */
	public static final String MESSAtGE_IMAGE = "image";
	/**
	 * 图文消息
	 */
	public static final String MESSAGE_NEWS = "news";
	/**
	 * 语音消息
	 */
	public static final String MESSAGE_VOICE = "voice";
	/**
	 * 视频消息
	 */
	public static final String MESSAGE_VIDEO = "video";
	/**
	 * 小视频消息
	 */
	public static final String MESSAGE_SHORTVIDEO = "shortvideo";
	/**
	 * 地理位置消息
	 */
	public static final String MESSAGE_LOCATION = "location";
	/**
	 * 链接消息
	 */
	public static final String MESSAGE_LINK = "link";
	/**
	 * 事件推送消息
	 */
	public static final String MESSAGE_EVENT = "event";
	/**
	 * 事件推送消息中,事件类型，subscribe(订阅)
	 */
	public static final String MESSAGE_EVENT_SUBSCRIBE = "subscribe";
	/**
	 * 事件推送消息中,事件类型，unsubscribe(取消订阅)
	 */
	public static final String MESSAGE_EVENT_UNSUBSCRIBE = "unsubscribe";
	/**
	 * 事件推送消息中,上报地理位置事件
	 */
	public static final String MESSAGE_EVENT_LOCATION_UP = "LOCATION";
	/**
	 * 事件推送消息中,自定义菜单事件,点击菜单拉取消息时的事件推送
	 */
	public static final String MESSAGE_EVENT_CLICK = "CLICK";
	/**
	 * 事件推送消息中,自定义菜单事件,点击菜单跳转链接时的事件推送
	 */
	public static final String MESSAGE_EVENT_VIEW = "VIEW";

	/**
	 * 将XML转换成Map集合
	 */
	public static Map< String, String > xmlToMap( HttpServletRequest request )
					throws IOException, DocumentException
	{

		Map< String, String > map = new HashMap< String, String >();
		SAXReader reader = new SAXReader(); // 使用dom4j解析xml
		InputStream ins = request.getInputStream(); // 从request中获取输入流
		Document doc = reader.read( ins );

		Element root = doc.getRootElement(); // 获取根元素
		List< Element > list = root.elements(); // 获取所有节点

		for ( Element e : list )
		{
			map.put( e.getName(), e.getText() );
			System.out.println( e.getName() + "--->" + e.getText() );
		}
		ins.close();
		return map;
	}

	/**
	 * 将文本消息对象转换成XML
	 */
	public static String textMessageToXML( TextMeaasge textMessage )
	{
		try
		{
			Document doc = DocumentHelper.createDocument();
			Element root = doc.addElement( "xml" );
			root.addElement( "ToUserName" ).addCDATA( textMessage.getToUserName() );
			root.addElement( "FromUserName" ).addCDATA( textMessage.getFromUserName() );

			root.addElement( "CreateTime" ).setText( textMessage.getCreateTime() );
			root.addElement( "MsgType" ).addCDATA( textMessage.getMsgType() );
			root.addElement( "Content" ).addCDATA( textMessage.getContent() );
			OutputFormat format = OutputFormat.createCompactFormat(); //
			StringWriter writer = new StringWriter();
			XMLWriter output = new XMLWriter( writer, format );
			output.write( doc.getRootElement() );
			writer.close();
			output.close();
			return writer.toString();
		}
		catch ( IOException e )
		{
			e.printStackTrace();
			return null;
		}
	}

}